package com.tina.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tina.entity.TiOnline;
import com.tina.entity.TiRole;
import com.tina.entity.TiUsers;
import com.tina.mapper.TiOnlineMapper;
import com.tina.service.TiOnlineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tina.util.DateUtils;
import com.tina.util.IpUtils;
import com.tina.util.ServletUtils;
import com.tina.util.TimeUtitl;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.session.mgt.WebSessionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 在线用户 服务实现类
 * </p>
 *
 * @author Tina
 * @since 2020-06-08
 */
@Service
@Transactional
public class TiOnlineServiceImpl extends ServiceImpl<TiOnlineMapper, TiOnline> implements TiOnlineService {
    @Autowired
    private SessionDAO sessionDAO;

    @Autowired
    private TiOnlineMapper onlineMapper;

    private Logger logger = LoggerFactory.getLogger(TiOnlineServiceImpl.class);

    /**
     * 查看所有在线用户
     *
     * @param
     * @return
     */
    @Override
    public IPage<TiOnline> queryOnlineUser(TiOnline online, Integer page, Integer limit) {
        AbstractWrapper wrapper = new QueryWrapper<TiOnline>();
        if (online != null) {
            if (!StringUtils.isEmpty(online.getOnlineLoginName())) {
                wrapper.like("online_login_name", online.getOnlineLoginName());
            }
        }
        wrapper.orderByDesc("online_start_timestamp");
        Page<TiOnline> iPage = new Page<TiOnline>(page, limit);
        Page selectPage = onlineMapper.selectPage(iPage, wrapper);
        return selectPage;
    }

    /**
     * 根据SessionId踢出用户
     * Session session = sessionDAO.readSession(sessionId);
     * sessionDAO.delete(session);
     *
     * @param sessionId
     * @return
     */
    @Override
    public boolean forceLogout(String sessionId) {
        Session session = sessionDAO.readSession(sessionId);
        sessionDAO.delete(session);
        TiOnline online = new TiOnline();
        online.setOnlineSessionId(sessionId);
        online.setOnlineState(1);
        Integer editOnlineStateById = onlineMapper.updateById(online);
        logger.info("editOnlineStateById={}", editOnlineStateById);
        if (editOnlineStateById > 0) {
            return true;
        }
        return false;
    }


}
